package com.yw.swagger.controller;

import com.alibaba.fastjson.JSON;
import com.yw.swagger.customNote.fyh;
import com.yw.swagger.mapper.SysBpsGpmemlnkMapper;
import com.yw.swagger.model.Evection;
import com.yw.swagger.model.Records;
import com.yw.swagger.model.SysBpsGpmemlnk;
import com.yw.swagger.model.Utree;
import com.yw.swagger.service.RecordsService;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.Global;
import com.yw.swagger.tool.Utils.HttpClientUtils;
import com.yw.swagger.tool.controller.BaseController;
import com.yw.swagger.tool.page.TableDataInfo;
import io.swagger.annotations.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: Abeiv
 * @Description:
 * @Date: 2019-10-29 11:45
 */
@Api(tags = "授课和出差备案")
@Controller
@RequestMapping("/goOut")
public class RecordsController extends BaseController {

    @Autowired
    private SysBpsGpmemlnkMapper sysBpsGpmemlnkMapper;
    @Autowired
    RecordsService recordsService;
    @fyh
    @ApiOperation(notes = "外出授课备案", value = "外出授课备案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", example = "A8383U3243"),
            @ApiImplicitParam(name = "user", value = "账号"),
            @ApiImplicitParam(name = "pwd", value = "密码"),
            @ApiImplicitParam(name = "action", value = "操作"),
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/lessons")
    @ResponseBody
    public AjaxResult lessons(String user,String pwd,String action,Records records) {
        SysBpsGpmemlnk Gpmemlnk= sysBpsGpmemlnkMapper.selectGroundByAca(user);
        records.setUserName(Gpmemlnk.getGpmemUserdspname());
        records.setFwDept(Gpmemlnk.getGpmemGroup()+"-"+Gpmemlnk.getGpmemUserdspname());
        records.setFwTitle("("+records.getFwDept()+")外出授课备案");
        Map map=getFiledInfo(records);
        String json= JSON.toJSONString(map);
        System.err.println("json:"+json);
        return requestService("QTYwWcShouKeFirstPost","user,pwd,JsonContent", user+",,"+pwd+",,"+json);
    }


    @fyh
    @ApiOperation(notes = "外出出差备案", value = "外出出差备案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", example = "A8383U3243"),
            @ApiImplicitParam(name = "user", value = "账号"),
            @ApiImplicitParam(name = "pwd", value = "密码"),
            @ApiImplicitParam(name = "action", value = "操作"),
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/evection")
    @ResponseBody
    public AjaxResult requestService(String user,String pwd,String action,Evection evection) {
        SysBpsGpmemlnk Gpmemlnk=sysBpsGpmemlnkMapper.selectGroundByAca(user);
        evection.setUserName(Gpmemlnk.getGpmemUserdspname());
        evection.setFwDept(Gpmemlnk.getGpmemGroup()+"-"+Gpmemlnk.getGpmemUserdspname());
        evection.setFwTitle("("+evection.getFwDept()+")出差备案");
        Map map=getFiledInfo(evection);
        String json= JSON.toJSONString(map);
        System.err.println("json:"+json);
        return requestService("QTYwZcChuChaiFirstPost","user,pwd,JsonContent", user+",,"+pwd+",,"+json);
    }





    @fyh
    @ApiOperation(notes = "档案类型", value = "档案类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", example = "A8383U3243"),
            @ApiImplicitParam(name = "deptName", value = "所属部门", example = "办公室"),
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/fileType")
    @ResponseBody
    public AjaxResult fileType(String deptName) {
        //查询办公室档案
        List<Utree>list= recordsService.selectFileType(deptName);
        TableDataInfo td = getDataTable(list);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }



    public static Map<String,Object> getFiledInfo(Object o) {
        Map<String,Object> parameters = new HashMap<>();
        Field[] fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            parameters.put(field.getName(), getFieldValueByName(field.getName(), o));
        }
        return parameters;
    }
    public static  Object getFieldValueByName(String fieldName,Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            return null;
        }
    }

    public AjaxResult requestService(String methodName, String paramName, String paramValue) {
        String endpoint = Global.getConfig("yw.webService");
        try {
            String[] paramNames = paramName.split(",");
            String[] paramValues = paramValue.split(",,");
            if (paramNames.length != paramValues.length) {
                return AjaxResult.error("参数不正确");
            } else {
                Map map = new HashMap();
                for (int i = 0; i < paramNames.length; i++) {
                    map.put(paramNames[i], paramValues[i]);
                }
                HttpClientContext clientContext = HttpClientContext.create();
                String s = HttpClientUtils.httpPost(endpoint + "/" + methodName, map, 10000, clientContext);
                AjaxResult aj = new AjaxResult(AjaxResult.Type.SUCCESS, null);
                System.err.println("s:"+s);
                aj.put("data", s);
                return aj;
            }
        } catch (Exception e) {
            return AjaxResult.error("服务器运行错误");
        }
    }


}
